﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 字符串处理工具.UserControlList
{
    /// <summary>
    /// StringConvert.xaml 的交互逻辑
    /// </summary>
    public partial class StringConvert : UserControl
    {
        public StringConvert()
        {
            InitializeComponent();
        }


        #region Unicode 转换相关

        #region Unicode  ASCII
        private void SCUnicodeASCIIToUnicodeButton_Click(object sender, RoutedEventArgs e)
        {
            string tempUnicode = "";
            string ascii = SCUnicodeTextBox.Text;
            SCUnicodeTextBoxTo.Text = tempUnicode;
        }
        #endregion

        /// <summary>
        /// Unicode 字符串 转 汉字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SCUnicodeUnicodeToCNButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(SCUnicodeTextBoxTo.Text))
                {
                    SCUnicodeTextBox.Text = UnicodeToString(SCUnicodeTextBoxTo.Text);
                }
            }
            catch (Exception ex)
            {
                SCUnicodeTextBox.Text = ex.Message;
            }
        }

        /// <summary>
        /// 汉字 转 Unicode 字符串
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SCUnicodeCNToUnicodeButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(SCUnicodeTextBox.Text))
                {
                    SCUnicodeTextBoxTo.Text = StringToUnicode(SCUnicodeTextBox.Text);
                }
            }
            catch (Exception ex)
            {
                SCUnicodeTextBoxTo.Text = ex.Message;
            }

        }
        #endregion

        #region 清楚文本框中的内容
        /// <summary>
        /// 清楚文本框中的内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SCCleanButton_Click(object sender, RoutedEventArgs e)
        {
            string tempUnicode = "";
            SCUnicodeTextBox.Text = tempUnicode;
            SCUnicodeTextBoxTo.Text = tempUnicode;
            SCUTFTextBox.Text = tempUnicode;
            SCUTFTextBoxTo.Text = tempUnicode;
            SCURLTextBox.Text = tempUnicode;

        }
        #endregion

        #region UTF-8
        private void SCUTF8ToCNButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SCUTFTextBoxTo.Text = StringToUTF82(SCUTFTextBox.Text);
            }
            catch (Exception ex)
            {
                SCUTFTextBoxTo.Text = ex.Message;
            }
        }

        private void SCUTF8CNToUTF8Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {

            }
        }

        #endregion

        #region 私有方法
        private static string StringToUTF8(string unicodeString)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(unicodeString);
            String decodedString = utf8.GetString(encodedBytes);
            return decodedString;
        }

        private string StringToUTF81(string str)
        {
            byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(str);
            string str1 = "";
            foreach (byte b in buffer) str1 += string.Format("%{0:X}", b);
            return str1;
        }
        private string StringToUTF82(string str)
        {
            string temp = "";

            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(str);
            for (int ctr = 0; ctr < encodedBytes.Length; ctr++)
            {
                temp += encodedBytes[ctr];
            }
            String decodedString = utf8.GetString(encodedBytes);
            return temp;
        }
        /// <summary>
        /// 字符串转Unicode码
        /// </summary>
        /// <returns>The to unicode.</returns>
        /// <param name="value">Value.</param>
        private string StringToUnicode(string value)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(value);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i += 2)
            {
                // 取两个字符，每个字符都是右对齐。
                stringBuilder.AppendFormat("\\u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// Unicode转字符串
        /// </summary>
        /// <returns>The to string.</returns>
        /// <param name="unicode">Unicode.</param>
        private string UnicodeToString(string unicode)
        {
            string resultStr = "";
            string[] strList = unicode.Split(new String[] { "\\u" }, StringSplitOptions.None);
            for (int i = 1; i < strList.Length; i++)
            {
                resultStr += (char)int.Parse(strList[i], System.Globalization.NumberStyles.HexNumber);
            }
            return resultStr;
        }

        #endregion

        #region URL 编码
        private void SCUUrlEncodeButton_Click(object sender, RoutedEventArgs e)
        {
            string type = GetSCType();
            switch (type)
            {
                case "UTF-8":
                    SCURLTextBox.Text = System.Web.HttpUtility.UrlEncode(SCURLTextBox.Text, System.Text.Encoding.UTF8);
                    break;
                case "Unicode":
                    SCURLTextBox.Text = System.Web.HttpUtility.UrlEncode(SCURLTextBox.Text, System.Text.Encoding.Unicode);
                    break;
                case "GB2312":
                    SCURLTextBox.Text = System.Web.HttpUtility.UrlEncode(SCURLTextBox.Text, System.Text.Encoding.GetEncoding("GB2312"));
                    break;
            }
        }

        private void SCUrlDecodeButton_Click(object sender, RoutedEventArgs e)
        {
            string type = GetSCType();
            switch (type)
            {
                case "UTF-8":
                    SCURLTextBox.Text = System.Web.HttpUtility.UrlDecode(SCURLTextBox.Text, System.Text.Encoding.UTF8);
                    break;
                case "Unicode":
                    SCURLTextBox.Text = System.Web.HttpUtility.UrlDecode(SCURLTextBox.Text, System.Text.Encoding.Unicode);
                    break;
                case "GB2312":
                    SCURLTextBox.Text = System.Web.HttpUtility.UrlDecode(SCURLTextBox.Text, System.Text.Encoding.GetEncoding("GB2312"));
                    break;
            }
        }

        private string GetSCType()
        {
            if (SCUTF8Radio.IsChecked.HasValue && SCUTF8Radio.IsChecked.Value)
            {
                return "UTF-8";
            }
            if (SCUnicodeRadio.IsChecked.HasValue && SCUnicodeRadio.IsChecked.Value)
            {
                return "Unicode";
            }
            if (SCGB2312Radio.IsChecked.HasValue && SCGB2312Radio.IsChecked.Value)
            {
                return "GB2312";
            }
            return "";
        }
        #endregion

        #region 时间戳

        #endregion

        private void SCUnixButton_Click(object sender, RoutedEventArgs e)
        {
            string str = "";
            DateTime time = DateTime.Now;
            str += "当前北京时间为：" + time.ToString("yyyy-MM-dd HH:mm:ss");
            str += "\nUnix时间戳10位秒：" + GetTimeStamp(time, 10).ToString();
            str += "\nUnix时间戳13位毫秒：" + GetTimeStamp(time).ToString();
            SCUnixTextBoxALL.Text = str;
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public string GetTimeStamp(System.DateTime time, int length = 13)
        {
            long ts = ConvertDateTimeToInt(time);
            return ts.ToString().Substring(0, length);
        }

        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public long ConvertDateTimeToInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;
        }



    }
}
